// 初始化I级机构下拉菜单
var wage_detail_name;
var dnme;
var money = /^([1-9]\d{0,9}|0)([.]?|(\.\d{1,2})?)$/;
var path = $("#path").val();
$
		.ajax({
			type : "POST",
			url : "getOg_Departmentlist.json",
			dataType : "json",
			success : function(data) {
				if (data != null) {
					var depid = $("#oz_department_id").val();
					$("#oz_department").html("");
					var options = "<option value=\"0\">请选择</option>";
					for (var i = 0; i < data.length; i++) {
						if (depid != null && depid != undefined
								&& data[i].id == depid) {
							options += "<option selected=\"selected\" value=\""
									+ data[i].id + "\" >"
									+ data[i].oz_department + "</option>";
						} else {
							options += "<option value=\"" + data[i].id + "\" >"
									+ data[i].oz_department + "</option>";
						}
					}
					$("#oz_department").html(options);
				}
			}
		});
function Mesload() {
	$('#table')
			.bootstrapTable(
					{
						method : "POST",
						url : "cp_payoff_grant.json",
						striped : true,
						singleSelect : false,
						dataType : "json",
						pagination : true, // 分页
						pageSize : 10,
						pageNumber : 1,
						search : false, // 显示搜索框
						contentType : "application/x-www-form-urlencoded",
						queryParams : null,
						columns : [
								{
									checkbox : "true",
									field : 'id',
									align : 'center',
									valign : 'middle'
								},
								{
									title : '工资单编码',
									field : 'sl_number',
									align : 'center'

								},
								{
									title : '工资单状态',
									field : 'st_State',
									align : 'center'

								},

								{
									title : "I级机构名称",
									field : 'oz_department',
									align : 'center',
									valign : 'middle',

								},
								{
									title : 'II级机构名称',
									field : 'p_ClassName',
									align : 'center',
									valign : 'middle'
								},
								{
									title : '人数',
									field : 'countNum',
									align : 'center',

								},

								{
									title : '待发薪酬总额',
									field : 'totalMoney',
									align : 'center',
									valign : 'middle',

								},
								{
									title : '操作',
									field : '',
									align : 'center',

									formatter : function(value, row) {
										var detail = '<a href="#" onclick="detail(\''
												+ row.sl_number
												+ '\')">查看明细</a> '
									  return detail;
									}
								} ],
						onEditableSave : function(field, row, oldValue, $el) {
							var rows = {
								index : 1, // 更新列所在行的索引
								field : "sumEm_totalmoney", // 要更新列的field
								value : "INITIALIZATION" // 要更新列的数据
							}// 更新表格数据
							$('#table').bootstrapTable("updateCell", rows);
						}
					});

	getData();

}
  function getData() {
	 var oz_department=$("#oz_department").val();
	 var sl_number=$("#sl_number").val();
		$.ajax({
			type : "POST",
			url : "cp_payoff_grant.json",
			data:{oz_department:oz_department,sl_number:sl_number},		
			dataType : "json",
			success : function(data) {
				if (data != null) {
					$('#table').bootstrapTable("load", data);
				}
			}
		});
	  
}
  

  function detail(wage_detail_name) {
  	wage_detail_name = wage_detail_name;
  	$("#light").css("display", "inline");
  	$("#fade").css("display", "inline");
  	 $("#btn").css("display", "inline");
  	$.ajax({
  		type : "GET",
  		url : "cp_payoff_index.json?wage_detail_name=" + wage_detail_name,
  		dataType : "json",
  		success : function(data) {
  			if (data != null) {

  				$("#span_wage_detail_name").html(data[0].wage_detail_name);
  				$("#span_oz_department").html(data[0].oz_department);
  				$("#span_countPersonnelfile_id").html(
  						data[0].countPersonnelfile_id);
  				$("#span_sumEm_totalmoney").html(data[0].sumEm_totalmoney);
  				$("#span_sendTotalMoney").html("");
  			}
  		}
  	})
  	getPayDate();
  	$('#table1').bootstrapTable(
  			{

  				method : "POST",
  				url : "getSendPay.json",
  				striped : true,
  				singleSelect : false,
  				dataType : "json",
  				search : false, // 显示搜索框
  				contentType : "application/x-www-form-urlencoded",
  				queryParams : function(params) { // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
  					return {
  						pageSize : params.limit, // 每页要显示的数据条数
  						offset : params.offset, // 每页显示数据的开始行号
  						sort : params.sort, // 要排序的字段
  						sortOrder : params.order, // 排序规则
  						wage_detail_name : wage_detail_name
  					// 额外添加的参数
  					}
  				},

  				columns : [ {
  					title : '姓名',
  					field : 'pf_name',
  					align : 'center'
  				}, {
  					title : '档案编码',
  					field : 'pid',
  					align : 'center',
  					valign : 'middle',

  				}, {
  					title : "基本工资",
  					field : 'em_basemoney',
  					align : 'center',
  					valign : 'middle'

  				}, {
  					title : '餐补',
  					field : 'em_mealmaoney',
  					align : 'center',
  					valign : 'middle'

  				}, {
  					title : '住房补助',
  					field : 'em_housingmoney',
  					align : 'center',
  					valign : 'middle'
  				}

  				, {
  					title : '绩效工资',
  					field : 'sl_bonus',
  					align : 'center',
  					valign : 'middle',
  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false)
  								return '绩效工资输入非法或为空！';
  						}
  					},

  				}, {
  					title : '交通补助',
  					field : 'sl_traffic',
  					align : 'center',
  					valign : 'middle',

  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false)
  								return '交通补助输入非法或为空！';
  						}
  					},

  				},

  				{
  					title : '通讯补助',
  					field : 'sl_communication',
  					align : 'center',
  					valign : 'middle',
  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false)
  								return '通讯补助输入非法或为空！';
  						}
  					},

  				},

  				{
  					title : '出差补助',
  					field : 'sl_evection',
  					align : 'center',
  					valign : 'middle',
  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false)
  								return '出差补助输入非法或为空！';
  						}
  					},
  				}, {
  					title : '加班补助',
  					field : 'sl_overtime',
  					align : 'center',
  					valign : 'middle',
  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false)
  								return '加班补助输入非法或为空！';
  						}
  					},

  				}, {
  					title : '其他补助',
  					field : 'sl_else',
  					align : 'center',
  					valign : 'middle',
  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false) {
  								return '其他补助输入非法或为空！';
  							}
  						}
  					},

  				}, {
  					title : '应扣金额',
  					field : 'sl_deduct',
  					align : 'center',
  					valign : 'middle',
  					editable : {
  						type : "text",
  						onblur : "submit",
  						showbuttons : true,
  						validate : function(v) {
  							if (!v || money.test(v) == false)
  								return '应扣金额输入非法或为空！';
  						}
  					},

  				}, {
  					title : '实发工资',
  					field : 'sl_reality',
  					align : 'center',
  					valign : 'middle',
  				} ],
  				onEditableSave : function(field, row, oldValue, $el) {
  					var rows = {
  						index : esIndex, // 更新列所在行的索引
  						field : "sl_reality", // 要更新列的field
  						value : row.em_basemoney + row.em_mealmaoney
  								+ row.em_housingmoney
  								+ parseFloat(row.sl_bonus)
  								+ parseFloat(row.sl_traffic)
  								+ parseFloat(row.sl_communication)
  								+ parseFloat(row.sl_evection)
  								+ parseFloat(row.sl_overtime)
  								+ parseFloat(row.sl_else)
  								- parseFloat(row.sl_deduct) // 要更新列的数据
  					}// 更新表格数据
  					$('#table1').bootstrapTable("updateCell", rows);
  				}

  			});

  	var esIndex;
  	$('#table1').on("click-row.bs.table", function(e, row, $element) {
  		esIndex = $element.data('index');

  	});
  	function getPayDate() {
  		dnme = wage_detail_name;
  		$.ajax({
  			type : "GET",
  			url : "getSendPay.json?wage_detail_name="
  					+ wage_detail_name,
  			dataType : "json",
  			success : function(result) {
  				if (result != null) {
  					var TableData = result;
  					$('#table1').bootstrapTable("load", TableData);
  				}
  			}
  		})
  	}

  }
  function audit() {
		var rows = $('#table1').bootstrapTable('getData');// 行的数据
		var ids = "";
		var payofflist = [];
		for (var i = 0; i < rows.length; i++) {
			if (rows[i].sl_reality == null || rows[i].sl_reality == "") {
				alert("还有未填写的员工工资单！");
				return;
			} else {
				var payoff = {
					"sl_number" : dnme,
					"sl_dgdh" : rows[i].pid,
					"sl_bonus" : rows[i].sl_bonus,
					"sl_traffic" : rows[i].sl_traffic,
					"sl_communication" : rows[i].sl_communication,
					"sl_evection" : rows[i].sl_evection,
					"sl_overtime" : rows[i].sl_overtime,
					"sl_else" : rows[i].sl_else,
					"sl_deduct" : rows[i].sl_deduct,
					"sl_reality" : rows[i].sl_reality
				};
				payofflist.push(payoff);

			}
		}
		$.ajax({
			type : "POST",
			url : "updatePayofflist.json",
			data : {
				payoff : JSON.stringify(payofflist)
			},
			dataType : "json",
			success : function(data) {
				if (data.result == true) {
					alert("审核成功！工资会在近日进行发放");
					$("#light").css("display", "none");
					$("#fade").css("display", "none");
					getData();
				} else {
					alert("审核失败！")
				}
			}
		})
	}